# 获取优惠券列表
import json
from django_redis import get_redis_connection
from doctors.models import DoctorModel


# 获取当前用户的所有优惠券：使用、未使用、已过期
def get_user_coupons_list(user_id):
    # 获取redis连接
    redis = get_redis_connection("coupon")
    # 查找所有以特定user_id开头的键
    print('用户id', user_id)
    coupon_list = redis.keys(f'{user_id}:*')

    print('查看用户的所有优惠券', coupon_list)

    # 将一个包含字节对象的列表 coupon_list 转换为一个包含字符串对象的列表 coupon_id_list
    coupon_id_list = [c.decode() for c in coupon_list]

    coupon_data = []
    for coupon_key in coupon_id_list:
        # 1:14
        # 获取优惠券id信息
        coupon_item = {'user_coupon_id': int(coupon_key.split(':')[-1])}
        # 根据当前的key，获取对应的优惠券属性信息
        coupon_hash = redis.hgetall(coupon_key)
        # 循环coupon_hash，获取所有属性信息
        for key, value in coupon_hash.items():
            key = key.decode()
            value = value.decode()
            # 判断 key 获取到的key为外键情况下，需要对value进行json处理
            if key in ['to_doctors', 'to_departments', 'to_hospitals']:
                value = json.loads(value)
            coupon_item[key] = value
        coupon_data.append(coupon_item)
    print('所有优惠券', coupon_data)
    return coupon_data


# 获取当前用户有效的优惠券列表：购买的时候  当前下单可用的优惠券
def get_user_valid_coupons_list(user_id, doctor_id):
    # 获取当前用户本次下单的优惠券列表
    # 获取当前用户下单的需要咨询的医生

    doctor_qs = DoctorModel.objects.filter(id=doctor_id)

    # 便利医生，获取医生所属的医院和科室
    department_ids = set()
    hospital_ids = set()
    doctor_ids = set()

    for doctor in doctor_qs:
        if doctor.department:
            department_ids.add(doctor.department.id)
            print('科室id', department_ids)
        if doctor.hospital:
            hospital_ids.add(doctor.hospital.id)
            print('医院id', hospital_ids)
        if doctor.id:
            doctor_ids.add(doctor.id)
            print('医生id', doctor_ids)
    # 获取当前用户所有的优惠券列表
    user_coupons_list = get_user_coupons_list(user_id)
    # 构建一个可用的优惠券的空对象，用于存储可用的优惠券
    valid_coupons_list = []
    # 遍历用户的所有优惠券,判断是否满足条件
    print('2222222222222222222222用户优惠券列表', user_coupons_list)
    for user_coupon in user_coupons_list:
        coupon_type = int(user_coupon.get('coupon_type'))
        print('优惠券类型', coupon_type)


        if coupon_type == 0:
            # 全场通用
            user_coupon['enable_doctor'] = '__all__'
            valid_coupons_list.append(user_coupon)
            print('全场通用优惠券11111111111111111111111111111111111', valid_coupons_list)
        elif coupon_type == 3:
            # 指定医院
            # 获取当前用户的优惠券所属的医院
            coupon_hospitals = {hospital_item['hospital__id'] for hospital_item in user_coupon.get('to_hospitals')}
            print(
                '------------------------------------------------医院优惠券------------------------------------------------')
            print('优惠券所属的医院', coupon_hospitals)
            # 将优惠券所属的医院和当前用户下单的医院进行交集运算，即存在相同的id
            ret = coupon_hospitals & hospital_ids

            if len(ret) > 0:
                # 可用的医生id
                user_coupon['enable_doctor'] = {int(doctor.id) for doctor in doctor_qs if doctor.hospital.id in ret}
                valid_coupons_list.append(user_coupon)

        elif coupon_type == 1:
            # 指定科室
            # 获取当前用户的优惠券所属的科室
            print('5555555555555555', user_coupon.get('to_departments'))
            coupon_departments = {department_item['department__id'] for department_item in
                                  user_coupon.get('to_departments')}

            print(
                '-----------------------------------------科室优惠券-------------------------------------------------------')

            print('优惠券所属的科室', coupon_departments)
            # 将优惠券所属的科室和当前用户下单的科室进行交集运算，即存在相同的id
            ret = coupon_departments & department_ids
            if len(ret) > 0:
                # 可用的医生id
                user_coupon['enable_doctor'] = {int(doctor.id) for doctor in doctor_qs if doctor.department.id in ret}
                valid_coupons_list.append(user_coupon)

        elif coupon_type == 2:
            # 指定医生
            # 获取当前用户的优惠券所属的医生
            print('6666666666666666', user_coupon.get('to_doctors'))
            coupon_doctors = {doctor_item['doctor__id'] for doctor_item in user_coupon.get('to_doctors')}
            print(
                '---------------------------------------------------医生优惠券---------------------------------------------')

            print('优惠券所属的医生', coupon_doctors)
            # 将优惠券所属的医生和当前用户下单的医生进行交集运算，即存在相同的id
            ret = coupon_doctors & doctor_ids
            if len(ret) > 0:
                # 可用的医生id
                user_coupon['enable_doctor'] = {int(doctor.id) for doctor in doctor_qs if doctor.id in ret}
                valid_coupons_list.append(user_coupon)

    print('11111111可用优惠券1111111111111', valid_coupons_list)

    return valid_coupons_list